package com.ruoyi.system.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.House;
import com.ruoyi.system.domain.HouseOrder;
import com.ruoyi.system.service.IHouseOrderService;
import com.ruoyi.system.service.IHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 房源订购Controller
 *
 * @author ...
 * @date 2022-03-18
 */
@RestController
@RequestMapping("/system/order")
public class HouseOrderController extends BaseController {
    @Autowired
    private IHouseOrderService houseOrderService;
    @Autowired
    private IHouseService houseService;


    /**
     * 销量统计
     */
    @GetMapping("/manageCount")
    public AjaxResult manageCount() {
        return AjaxResult.success(houseOrderService.manageCount());
    }

    /**
     * 金额统计
     */
    @GetMapping("/startCount")
    public AjaxResult startCount() {
        return AjaxResult.success(houseOrderService.startCount());
    }

    /**
     * 查询房源订购列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HouseOrder houseOrder) {
        startPage();
        List<HouseOrder> list = houseOrderService.selectHouseOrderList(houseOrder);
        return getDataTable(list);
    }

    /**
     * 获取房源订购详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(houseOrderService.selectHouseOrderById(id));
    }

    /**
     * 新增房源订购
     */
    @PostMapping
    public AjaxResult add(@RequestBody HouseOrder houseOrder) {
        House house = houseService.selectHouseById(houseOrder.getHouseId());
        houseOrder.setHouseName(house.getName());
        houseOrder.setRentTime(DateUtils.getNowDate());
        houseOrder.setExpireTime(DateUtils.addMonths(DateUtils.getNowDate(),houseOrder.getMouthNum().intValue()));
        houseOrder.setCreateUser(SecurityUtils.getUserId().intValue());
        houseOrder.setCreateTime(DateUtils.getNowDate());
        houseOrder.setMoney(house.getPrice());
        int total = houseOrder.getMouthNum().intValue() * Integer.parseInt(house.getPrice());
        houseOrder.setTotalMoney(String.valueOf(total));
        return toAjax(houseOrderService.insertHouseOrder(houseOrder));
    }

    /**
     * 修改房源订购
     */
    @PutMapping
    public AjaxResult edit(@RequestBody HouseOrder houseOrder) {
        HouseOrder houseOrder1 = houseOrderService.selectHouseOrderById(houseOrder.getId());
        int num = houseOrder.getMouthNum().intValue();
        int total = Integer.parseInt(houseOrder1.getMoney()) * num;
        total = total+Integer.parseInt(houseOrder1.getTotalMoney());
        houseOrder.setTotalMoney(String.valueOf(total));
        houseOrder.setExpireTime(DateUtils.addMonths(houseOrder1.getExpireTime(),houseOrder.getMouthNum().intValue()));
        houseOrder.setUpdateUser(SecurityUtils.getUserId().intValue());
        houseOrder.setUpdateTime(DateUtils.getNowDate());
        return toAjax(houseOrderService.updateHouseOrder(houseOrder));
    }

    /**
     * 查询我的订购
     * @return
     */
    @GetMapping("/myOrder")
    public AjaxResult findMyOrder(){
        List<HouseOrder> myOrder = houseOrderService.findMyOrder();
        return AjaxResult.success(myOrder);
    }

    /**
     * 删除房源订购
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(houseOrderService.deleteHouseOrderByIds(ids));
    }
}
